package com.luban.authserver.config;

import com.luban.authserver.config.customfilter.VerifyCodeFilter;
import com.luban.authserver.config.customfilter.login.CustomAuthenticationManager;
import com.luban.authserver.config.customfilter.login.CustomAuthenticationProcessingFilter;
import com.luban.authserver.config.customfilter.login.CustomAuthenticationProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.preauth.AbstractPreAuthenticatedProcessingFilter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Fox
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {


    @Autowired
    private VerifyCodeFilter verifyCodeFilter;

    @Autowired
    private CustomAuthenticationProcessingFilter customAuthenticationProcessingFilter;
//

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }


    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("user1")
                .password(passwordEncoder().encode("123456"))
                .authorities("admin")
                .roles("role1");
        // auth.userDetailsService(userDetailsService);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 登录前加上验证吗验证
        http.addFilterBefore(verifyCodeFilter, AbstractPreAuthenticatedProcessingFilter.class);

        // 用户名密码认证
        http.addFilterAt(customAuthenticationProcessingFilter, UsernamePasswordAuthenticationFilter.class);

        http.formLogin()
                .and().authorizeRequests()
//                .antMatchers("/oauth/**").permitAll()
       // .antMatchers("/login", "/index").permitAll()
                .antMatchers("/user/getCurrentUser").permitAll()
                .anyRequest().authenticated()
                .and().csrf().disable();
    }


}
